package pers.cz.logtail.connect.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cz.config.JefConfiguration;
import pers.cz.logtail.connect.Connection;
import pers.cz.logtail.enums.DbConfigItem;
import pers.cz.util.ExceptionUtil;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @program: JefLogTail
 * @description: 数据库连接
 * @author: Cheng Zhi
 * @create: 2023-03-05 13:35
 **/
public class DbConnnection extends Connection {

    Logger logger = LoggerFactory.getLogger(DbConnnection.class);

    private static final String DEFAULT_INSERT_SQL = "insert into JEF_LOG_TAIL(content) values(?)";

    private static final String IP_FINLENAME_INSERT_SQL = "insert into JEF_LOG_TAILA(ip, fileName, content) values(?, ?, ?)";

    java.sql.Connection connection;

    public DbConnnection(DataSource dataSource) {
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            ExceptionUtil.rethrow(e);
        }
    }

    @Override
    public void close() {
        try {
            connection.close();
        } catch (SQLException e) {
            ExceptionUtil.rethrow(e);
        }
    }

    @Override
    public String get(String key) {
        return null;
    }

    @Override
    public Long autoIncrement(String key) {
        return null;
    }

    @Override
    public void expire(String key, Integer time) {

    }

    /**
     * 简单插入
     * @param content
     */
    @Override
    public void realInsert(String content) {
        PreparedStatement preparedStatement = null;
        String sql = JefConfiguration.get(DbConfigItem.DB_INSERT_SQL,DEFAULT_INSERT_SQL);
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, content);
            int count = preparedStatement.executeUpdate();
            if (count > 0) {
                logger.debug("保存成功" + count + "条");
            }
        } catch (SQLException e) {
            ExceptionUtil.rethrow(e);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void realSet(String key, String value) {

    }

    /**
     * 指定字段插入
     * @param key
     * @param content
     */
    @Override
    public void realInsert(String key, String content) {
        throw new IllegalArgumentException("DB模式暂不支持");
    }

    @Override
    public void realInsert(String ip, String fileName, String content) {
        PreparedStatement preparedStatement = null;
        String sql = JefConfiguration.get(DbConfigItem.DB_INSERT_SQL,IP_FINLENAME_INSERT_SQL);
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, ip);
            preparedStatement.setString(2, fileName);
            preparedStatement.setString(3, content);
            int count = preparedStatement.executeUpdate();
            if (count > 0) {
                logger.debug("保存成功" + count + "条");
            }
        } catch (SQLException e) {
            ExceptionUtil.rethrow(e);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
